package com.cqhilink.iot.jobstandardmanagement.services.outerservice.impl;

import com.cqhilink.iot.jobstandardmanagement.facade.utils.TreeIconSkin;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.EntityMapper;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.OrganizationMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.OutEntityService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.entity.JsmEntityService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.organization.JsmOrganizationService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.regionalmaintenance.RegionalMaintenanceService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.servicenode.JsmServiceNodeService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.station.JsmStationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YuPeng on 2016/12/15.
 * modify by HuangXueQian on 2017/03/17
 */

public class OutEntityServiceImpl implements OutEntityService {
    @Autowired
    private JsmEntityService entityService;
    @Autowired
    private EntityMapper entityMapper;
    @Autowired
    private OrganizationMapper orgMapper;
    @Autowired
    private JsmOrganizationService organizationService;
    @Autowired
    private JsmEntityService jsmEntityService;
    @Autowired
    private JsmStationService jsmStationService;
    @Autowired
    private RegionalMaintenanceService regionalMaintenanceService;
    @Autowired
    private JsmServiceNodeService jsmServiceNodeService;

    /**
     * @param isOrg
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public List<Ztree> getEntityTree(String isOrg, String id,Employee employee) throws Exception {
        List<Ztree> list = entityService.getEntityTree(isOrg, id,employee);
        return list;
    }

    /**
     * 获取实体信息
     *
     * @param enId 实体Id
     * @return
     */
    @Override
    public EntityVO getEntityByEntityId(String enId) throws Exception {
        return entityMapper.selectByEnId(enId);
    }

    /**
     * 获取某个生产实体的特性值
     *
     * @param enId
     * @return
     */
    @Override
    public List<EntityPro> getFeaturesByEntityId(String enId) {
        return entityService.getEntityProByEntityId(enId);
    }

    /**
     * 根据当前人员组织 获取实体
     * @param employee 人员信息
     * @param isOrg
     * @param keyword
     * @orgId 组织机构ID
     */
    @Override
    public List<Ztree> getEntityByEmployee(Employee employee, String isOrg, String orgId, String keyword) throws Exception {
        return entityService.getEntityOrgZtreeByEmployee(employee, isOrg, orgId, keyword);
    }

    @Override
    public List<Ztree> getEntityOrgDeptByEmployee(Employee employee, String isOrg, String orgId, String keyword) throws Exception {
        return entityService.getEntityOrgDeptZtreeByEmployee(employee, isOrg, orgId, keyword);
    }

    /**
     *
     * @param employee 人员信息
     * @param isOrg    是否组织
     * @param orgId    组织机构Id
     * @param keyword  搜索值
     * @param iconSkin 图标
     * @return
     * @throws Exception
     */
    @Override
    public List<Ztree> getEntityServiceNodeByEmployee(Employee employee, String isOrg, String orgId, String keyword, String iconSkin) throws Exception {
        List<Ztree> ztreeList = new ArrayList<>();

        //加载当前组织下的儿子组织和儿子生产实体
        if ("1".equals(isOrg)) {
            if(iconSkin.equals(TreeIconSkin.ICON_STATION.getDesc())){
                //异步加载当前组织的儿子组织：父查子
                List<Organization> organizationList = orgMapper.selectOrgByOrgPId(orgId);

                //组织机构转ztree元素：isParent均为true，可以异步加载
                List<Ztree> orgZtreeList = organizationService.setZtree(organizationList);
                //重新设置isParent
                for (Ztree ztree : orgZtreeList) {
                    jsmEntityService.setOrgZtreeIsParent(ztree, ztree.getId());
                }
                ztreeList.addAll(orgZtreeList);

                //加载当前组织下的生产实体
                List<Ztree> entities =  jsmEntityService.getEntityTreeById("1", orgId);
                //重新设置isParent
                for (Ztree ztree : entities) {
                    jsmEntityService.setEntityZtreeIsParent(ztree, ztree.getId());
                }
                ztreeList.addAll(entities);

                //场站下挂生产单元
                Station station = jsmStationService.selectByPrimaryKey(orgId);
                if(station != null && StringUtils.isNotEmpty(station.getServiceNodeId())){
                    ServiceNode serviceNode = jsmServiceNodeService.queryServiceNode(station.getServiceNodeId());
                    Ztree ztree = new Ztree();
                    ztree.setName(serviceNode.getNodeNames());
                    ztree.setId(station.getServiceNodeId());
                    ztree.setpId(orgId);
                    ztree.setIconSkin(TreeIconSkin.ICON_PRODUCTION_UNIT.getDesc());
                    List<Ztree> list = regionalMaintenanceService.selectChildRegionalMaintenanceZtree(station.getServiceNodeId());
                    if(list != null &  list.size()>0){
                        ztree.setIsParent(true);
                    }else{
                        ztree.setIsParent(false);
                    }

                    ztreeList.add(ztree);
                }
            }else if(iconSkin.equals(TreeIconSkin.IONC_WELL.getDesc())){
                //井下挂生产单元
                Station station = jsmStationService.selectByPrimaryKey(orgId);
                if(station != null && StringUtils.isNotEmpty(station.getServiceNodeId())){
                    ServiceNode serviceNode = jsmServiceNodeService.queryServiceNode(station.getServiceNodeId());
                    Ztree ztree = new Ztree();
                    ztree.setName(serviceNode.getNodeNames());
                    ztree.setId(station.getServiceNodeId());
                    ztree.setpId(orgId);
                    ztree.setIconSkin(TreeIconSkin.ICON_PRODUCTION_UNIT.getDesc());
                    List<Ztree> list = regionalMaintenanceService.selectChildRegionalMaintenanceZtree(station.getServiceNodeId());
                    if(list != null &  list.size()>0){
                        ztree.setIsParent(true);
                    }else{
                        ztree.setIsParent(false);
                    }

                    ztreeList.add(ztree);
                }
            }
        } else {
            //加载当前实体下的所有儿子实体
            if(iconSkin.equals(TreeIconSkin.ICON_PRODUCTION_ENTITY.getDesc())){
                List<Ztree> entities =  jsmEntityService.getEntityTreeById(isOrg, orgId);
                //重新设置isParent
                for (Ztree ztree : entities) {
                    jsmEntityService.setEntityZtreeIsParent(ztree, ztree.getId());
                }
                ztreeList.addAll(entities);
            //加载生产单元下的区域
            }else if(iconSkin.equals(TreeIconSkin.ICON_PRODUCTION_UNIT.getDesc())){
                List<Ztree> list = regionalMaintenanceService.selectChildRegionalMaintenanceZtree(orgId);
                for(Ztree ztree : list){
                    ztree.setIconSkin(TreeIconSkin.ICON_PROCESS_UNIT.getDesc());
                }
                ztreeList.addAll(list);
            //加载区域下的区域
            }else if(iconSkin.equals(TreeIconSkin.ICON_PROCESS_UNIT.getDesc())){
                List<Ztree> list = regionalMaintenanceService.selectChildRegionalMaintenanceZtree(orgId);
                for(Ztree ztree : list){
                    ztree.setIconSkin(TreeIconSkin.ICON_PROCESS_UNIT.getDesc());
                }
                ztreeList.addAll(list);
            }
        }

        return  ztreeList;
    }

}
